package com.wxd.modules.sys.controller;

import com.wxd.annotation.SysLog;
import com.wxd.modules.sys.entity.SysDeptEntity;
import com.wxd.modules.sys.service.SysDeptService;
import com.wxd.utils.Constant;
import com.wxd.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * 部门管理
 */
@RestController
@RequestMapping("/sys/dept")
public class SysDeptController  extends AbstractController {
    @Autowired
    private SysDeptService sysDeptService;

    /**
     * 部门列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:dept:list")
    public List<SysDeptEntity> list()
    {
        List<SysDeptEntity> deptList = sysDeptService.queryList(new HashMap<String,Object>());
        return deptList;
    }
    /**
     * 选择部门(添加、修改菜单)
     */
    @RequestMapping("/select")
    @RequiresPermissions("sys:dept:select")
    public R select(){
        List<SysDeptEntity> deptList = sysDeptService.queryList(new HashMap<String, Object>());

       /*//添加一级部门
        if(getUserId() == Constant.SUPER_ADMIN){
            SysDeptEntity root = new SysDeptEntity();
            root.setDeptId(0L);
            root.setName("一级部门");
            root.setParentId(-1L);
            root.setOpen(true);
            deptList.add(root);
        }*/

        return R.ok().put("deptList", deptList);
    }

    /**
     * 上级部门Id（管理员为0）
     */
    @RequestMapping("/info")
    @RequiresPermissions("sys:dept:list")
    public R info(){
        long deptId = 0;
        if(getUserId() != Constant.SUPER_ADMIN){
            List<SysDeptEntity> deptList = sysDeptService.queryList(new HashMap<String, Object>());
            Long parentId = null;
            for(SysDeptEntity sysDeptEntity : deptList){
                if(parentId == null){
                    parentId = sysDeptEntity.getParentId();
                    continue;
                }

                if(parentId > sysDeptEntity.getParentId().longValue()){
                    parentId = sysDeptEntity.getParentId();
                }
            }
            deptId = parentId;
        }

        return R.ok().put("deptId", deptId);
    }
    /**
     * 信息
     */
    @RequestMapping("/info/{deptId}")
    @RequiresPermissions("sys:dept:info")
    public R info(@PathVariable("deptId") Long depId)
    {
        SysDeptEntity dept = sysDeptService.getById(depId);
        return R.ok().put("dept",dept);
    }
    /**
     *保存
     */
    @SysLog("保存部门")
    @RequestMapping("/save")
    @RequiresPermissions("sys:dept:save")
    public R save(@RequestBody SysDeptEntity dept)
    {
          sysDeptService.save(dept);
          return R.ok();
    }
    /**
     * 修改
     */
    @SysLog("修改部门")
    @RequestMapping("/update")
    @RequiresPermissions("sys:dept:update")
    public R update(@RequestBody SysDeptEntity dept)
    {
        sysDeptService.updateById(dept);
        return R.ok();
    }
    /**
     * 删除
     */
    @SysLog("删除部门")
    @RequestMapping("/delete")
    @RequiresPermissions("sys:dept:delete")
    public R delete(Long deptId)
    {
        //判断是否有子部门
        List<Long> deptIdList = sysDeptService.queryDeptIdList(deptId);
        if(deptIdList.size()>0)
        {
            return R.error("请先删除子部门");
        }
        sysDeptService.removeById(deptId);
        return R.ok();
    }
}
